package com.atmilan.quartz.sync.pull.buyer.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.atmilan.quartz.model.entity.buyer.BuyerProductSku;
import com.atmilan.quartz.sync.pull.buyer.mapper.BuyerProductSkuMapper;
import com.atmilan.quartz.sync.pull.buyer.service.IBuyerProductSkuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hupeng
 * @since 2020-04-27
 */
@Slf4j
@Service
public class BuyerProductSkuServiceImpl extends ServiceImpl<BuyerProductSkuMapper, BuyerProductSku>
        implements IBuyerProductSkuService {
    @Autowired
    BuyerProductSkuMapper buyerProductSkuMapper;
    @Override
    public List<BuyerProductSku> getDeleteList(String goodsId, Date startTime) {
        QueryWrapper<BuyerProductSku> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id", goodsId);
        if (startTime != null) {
            queryWrapper.lt("create_time", startTime);
        }

        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<String> findDeleteSkuIds(Date startTime){
        return buyerProductSkuMapper.findDeleteSkuIds(startTime);
    }

    @Override
    public List<String> findAllSkuIds() {
        List<Object> list = buyerProductSkuMapper.selectObjs(new QueryWrapper<BuyerProductSku>().select("sku_id"));
        return list == null?new ArrayList<String>():list.stream().map(o->o.toString()).collect(Collectors.toList());
    }

    @Override
    public int removeBatchByGoodsIds(List<String> goodsIds) {
        if(CollectionUtils.isEmpty(goodsIds)){
            return 0;
        }
        return buyerProductSkuMapper.deleteByGoodsIds(goodsIds);
    }

    @Override
    public int removeBatch(List<BuyerProductSku> skuList) {
        if (CollectionUtils.isEmpty(skuList)) {
            return 0;
        }
        return this.baseMapper.deleteBatchIds(skuList.stream()
                                              .map(BuyerProductSku::getSkuId)
                                              .collect(Collectors.toList()));
    }

    @Override
    public boolean replaceIntoSku(BuyerProductSku buyerProductSku) {
        if(buyerProductSku == null || StringUtils.isBlank(buyerProductSku.getSkuId())){
            log.warn("无效的sku, info: {}",buyerProductSku);
            return false;
        }
        return buyerProductSkuMapper.replaceIntoSku(buyerProductSku)>0;
    }

    @Override
    public List<BuyerProductSku> findByGoodsId(String goodsId) {
        if(StringUtils.isBlank(goodsId)){
            return new ArrayList<BuyerProductSku>();
        }
        return this.list(Wrappers.<BuyerProductSku>lambdaQuery().eq(BuyerProductSku::getGoodsId,goodsId));
    }

    @Override
    public void deleteAll() {
        buyerProductSkuMapper.deleteAll();
    }
    
    @Override
    public void copyAll() {
        buyerProductSkuMapper.deleteProductSkuBack();
        buyerProductSkuMapper.copyProductSkuBack();
    }
    
}
